package com.nuanshui.framework.utils;


import com.alibaba.dubbo.rpc.RpcContext;
import com.nuanshui.framework.behavior.BehaviorContext;
import com.nuanshui.framework.lang.SysStringUtils;
import org.slf4j.Logger;
import org.slf4j.MDC;



/**
 *标准日志工具，要求在业务中的所有打印日志的地方，都用此类中的方法
 * @author root
 *
 */
public class LogUtil {

    /**
     * 日志的唯一编号，为了方便一个业务模块日志放在不同的文件中，可以连起来看
     */
    private static ThreadLocal<String> logUniqueId = new ThreadLocal<String>();
    
    @SuppressWarnings("unused")
	private static ThreadLocal<String> username = new ThreadLocal<String>();
    
    @SuppressWarnings("unused")
	private static ThreadLocal<String> userId = new ThreadLocal<String>();

    public static void init(String username,String userId, String safeToken){
    	myThreadStart(null);
    	MDC.put("username",username);
    	MDC.put("userId", userId);
    	String value = logUniqueId.get();
        MDC.put("unique",value+"  "+username+" "+userId + " " + (safeToken == null ? "未获取到 safeToken" : safeToken));
        MDC.put("uniqueStr",value);
        BehaviorContext.getContext().put("unique", MDC.get("unique"));
        BehaviorContext.getContext().put("uniqueStr",value);
        
    	
    }
    public static String getUniqueId() {
        return logUniqueId.get();
    }

    /**
     * 设置日志的唯一标号到线程变量
     * @param previousLogId
     */
    public static void myThreadStart(String previousLogId) {
        if (SysStringUtils.isBlank(previousLogId)) {
            logUniqueId.set(UniqueIdGenerator.generateRandomStr(10));
        } else {
            logUniqueId.set(previousLogId + "_" + UniqueIdGenerator.generateRandomStr(3));
        }
        
       // System.out.println("mdc    "+MDC.get("unique"));
    }
    
    /**
     * 把线程中的唯一标号去掉
     */
    public static void destory() {
        logUniqueId.remove();
        MDC.remove("unique");
        MDC.remove("username");
        MDC.remove("userId");
        RpcContext.removeContext();
    }

    
    public static void trace(Logger logger, String msg) {
        if (logger.isTraceEnabled()) {
            logger.trace(msg);
        }
    }

   
    public static void trace(Logger logger, String msg, Object obj) {
        if (logger.isTraceEnabled()) {
            logger.trace(msg + " | " + obj.toString());
        }
    }

   
    public static void trace(Logger logger, String msg, Throwable t) {
        if (logger.isTraceEnabled()) {
            logger.trace(msg, t);
        }
    }

   
    public static void debug(Logger logger, String msg) {
        if (logger.isDebugEnabled()) {
            logger.debug(msg);
        }
    }

    
    public static void debug(Logger logger, String msg, Object obj) {
        if (logger.isDebugEnabled()) {
            logger.debug(msg + " | " + obj.toString());
        }
    }

    
    public static void debug(Logger logger, String msg, Throwable t) {
        if (logger.isDebugEnabled()) {
            logger.debug(msg, t);
        }
    }

    
    public static void info(Logger logger, String msg) {
        if (logger.isInfoEnabled()) {
            logger.info(msg);
        }
    }

   
    public static void info(Logger logger, String msg, Object obj) {
        if (logger.isInfoEnabled()) {
            if(obj!=null)
            {
                logger.info(msg + " | " + obj.toString());
            } else
            {
                logger.info(msg + " | " + obj);
            }
        }
    }
    
    public static void info(Logger logger, String msg, Throwable t) {
        if (logger.isInfoEnabled()) {
            logger.info(msg, t);
        }
    }

    
    public static void warn(Logger logger, String msg) {
        if (logger.isWarnEnabled()) {
            logger.warn(msg);
        }
    }

  
    public static void warn(Logger logger, String msg, Object obj) {
        if (logger.isWarnEnabled()) {
            logger.warn(msg + " | " + obj.toString());
        }
    }

  
    public static void warn(Logger logger, String msg, Throwable t) {
        if (logger.isWarnEnabled()) {
            logger.warn(msg, t);
        }
    }    
    public static void error(Logger logger, String msg) {
        if (logger.isErrorEnabled()) {
            logger.error(msg);
        }
    }

  
    public static void error(Logger logger, String msg, Object obj) {
        if (logger.isErrorEnabled()) {
            logger.error(msg + " | " + obj.toString());
        }
    }

    
    public static void error(Logger logger, String msg, Throwable t) {
        if (logger.isErrorEnabled()) {
            logger.error(msg, t);
        }
    }
}
